package com.longxiao.validator.handler;

import com.longxiao.validator.ValidException;
import com.longxiao.validator.ValidResult;
import com.longxiao.validator.handler.cond.CommCondition;

/**
 * Created by houhongwei on 2017/5/16.
 */
public class CommHandler implements ValidHandler {


    protected boolean isNull(Object value) {
        if (value == null) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    protected boolean isNotNull(Object value) {
        if (isNull(value)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    protected boolean isRequired(CommCondition cond) {
        if (cond.isRequired()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    protected String getName(CommCondition cond) {
        return cond.getName();
    }

    protected String getErr(CommCondition cond) {
        return cond.getErr();
    }


    @Override
    public ValidResult doValid(Object value, CommCondition cond) throws ValidException {
        if (isRequired(cond) && isNull(value)) {
            return ValidResult.createFail(getErr(cond));
        }
        return validOther(value, cond);
    }

    protected ValidResult validOther(Object value, CommCondition cond) throws ValidException {
        return ValidResult.createSucc();
    }

}
